var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');
import * as echarts from '../../utils/ec-canvas/echarts.js';

const app = getApp();

function setOption(chart, ge, zong) {

  var option = {
    backgroundColor: "#ffffff",
    color: ["#5AA250", "#FF7F27", "#67E0E3", "#91F2DE", "#FFDB5C", "#FF9F7F"],
    series: [{
      label: {
        normal: {
          fontSize: 14
        }
      },
      type: 'pie',
      center: ['50%', '50%'],
      radius: [0, '60%'],
      data: [{
        value: zong,
        name: '商户订单总数' + zong
      }, {
        value: ge,
        name: '你的订单数' + ge
      }, ],
      itemStyle: {
        emphasis: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 2, 2, 0.3)'
        }
      }
    }]
  };

  chart.setOption(option);
  return chart;
}


// pages/index1/index1.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      lazyLoad: true
    },
    infomation: {
      headImgUrl:"/static/images/toux1.jpg"
    },
    todayorder: {
      todayRatio: 0,
      orderRanking: 0,
      todayIncome: 0,
      totalIncome: 0,
      leaderOrder: 0,
      orderTotal: 0
    },
    a: 0,
    b: 0,
    c: 0,
    noworder: 1,
    monthorder: 1,
    waiteorder: 1
  },

  changge() {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },

  getzt() {
    let that = this
    util.request(api.QueryAppLeaderInfo, {}, 'get', {
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log("dasdad"+res.data)
      if(res.code==200){
      that.setData({
        infomation: res.data.leaderInfo
      })
      }
      else{
        wx.showModal({
          title: '错误提示',
          content: res.msg,
        })
      }
    })
  },
  //今日订单
  gettodayorder() {
    let that = this
    util.request(api.QueryTLInfo, {}, 'get', {
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log("kdmakdmakdakdmamdkadmakdmk")
      if (res.code == 200) {
        console.log("fff" + res.data)
        that.setData({
          todayorder: res.data
        })

      
        return false
      }
      if (res.code != 200) {
        wx.showModal({
          title: '错误提示',
          content: res.msg,
        })
      }


      that.selectComponent('#mychart-dom-pie').init((canvas, width, height) => {
        console.log(canvas)
        // 获取组件的 canvas、width、height 后的回调函数
        // 在这里初始化图表
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        setOption(chart, that.data.todayorder.leaderOrder, that.data.todayorder.orderTotal);

        // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
        that.chart = chart;

        that.setData({
          isLoaded: true,
          isDisposed: false
        });

        // 注意这里一定要返回 chart 实例，否则会影响事件处理等
        return chart;
      });

    })

  },

  // 获取本日订单
  getnownum() {
    let that = this
    util.request(api.LeaderOM + "/1", {

    }, 'get', {
      token: wx.getStorageSync("token")
    }).then(function(res) {
      console.log("今日" + res.data.order.length)
      that.setData({
        noworder: res.data.order.length
      })
    })
  },
  // 月订单
  getmonthordernum() {
    let that = this
    util.request(api.LeaderOM + "/2", {

    }, 'get', {
      token: wx.getStorageSync("token")
    }).then(function(res) {
      console.log("月" + res.data.order.length)
      that.setData({
        monthorder: res.data.order.length
      })
    })
  },
  //待提货
  getwaitenum() {
    let that = this
    util.request(api.LeaderOM + "/3", {

    }, 'get', {
      token: wx.getStorageSync("token")
    }).then(function(res) {
      console.log("月" + res.data.order.length)
      that.setData({
        waiteorder: res.data.order.length
      })
    })
  },
  gohome() {
    wx.navigateTo({
      url: '/pages/home/home',
    })
  },



  tixing() {
    wx.navigateTo({
      url: '/pages/sendgoods/sendgoods',
    })
  },


  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },


  // 订单
  d1() {
    this.setData({
      a: 1
    })
  },
  dd1() {
    this.setData({
      a: 0
    })
  },


  d2() {
    this.setData({
      b: 1
    })
  },
  dd2() {
    this.setData({
      b: 0
    })
  },


  d3() {
    this.setData({
      c: 1
    })
  },
  dd3() {
    this.setData({
      c: 0
    })
  },


  takeorder() {
    wx.showModal({
      title: '温馨提示',
      content: 'sorry，小程序该功能正在完善中！',
      showCancel: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.ecComponent = this.selectComponent('#mychart-dom-pie');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!wx.getStorageSync("token")) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    } else {
      this.getzt()
      this.gettodayorder()
      this.getnownum()
      this.getmonthordernum()
      this.getwaitenum()

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var shareObj = {
      title: '@所有人，我是团长aa，我今日用惠到家商家版赚了1000元，欢迎使用！',
      desc: '欢迎购物！',
      imageUrl: '/static/images/fenxiang.png',
      path: '/pages/home/home',
      success: function() {
        this.setData({
          openShare: false,
        });
      }
    }
    return shareObj
  }
})